 package com.action.app_center;

import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;


import com.dataProcess.Matter;
import com.enetiy.Area;
import com.enetiy.Areaip;
import com.enetiy.Useraccount;
import com.enetiy.Userinfo;
import com.mysql.fabric.xmlrpc.base.Data;
import com.operation.NET;
import com.service.AreaIpService;
import com.service.DealService;
import com.service.MapService;
import com.service.UseraccountService;
import com.service.UserinfoService;
import com.threads.NetoutThread;
import com.util.DataUtil;
import com.util.Log4gUtil;

public class LoginOutAction {
	private int userid;
	private String ip;
	private String token;
	
	private UserinfoService userinfoService;
	private AreaIpService areaIpService;
	private DealService dealService;
	private MapService mapService;
	
	public void setUseraccountService(UseraccountService useraccountService) {
		this.useraccountService = useraccountService;
	}

	private UseraccountService useraccountService;
	private String params;
	private String NF_callid;
	
 	/**
 	 * 手动退出登录 
 	 */
	public void loginOut(){
		
		System.out.println(params);
		
		if(params!=null&&!params.equals("")){
			JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("ip"))
				ip=datajson.getString("ip");
			if(datajson.has("userid"))
				userid =datajson.getInt("userid");
			
		}	
		Log4gUtil.Info("loginOut", userid+"", "退出登录", params, NF_callid, token);
		HttpServletResponse response = ServletActionContext.getResponse();
		net.sf.json.JSONObject j = DataUtil.sendJson();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			Userinfo ui = userinfoService.selectUserinfo(userid);
			
			//网络下线
			if(ui.getArea()!=null){
				List<Useraccount> accounts = useraccountService.selectUseraccounts(userid, 1);
				//异步下线
				if(!accounts.isEmpty()) {
					NetoutThread lt = new NetoutThread( ui, accounts.get(0).getAccount(),mapService, dealService,areaIpService,userid, NF_callid, token);
					Thread t1 = new Thread(lt); 
					t1.start();
				}
			}
			ui.setNetOnline(0);
			ui.setAppOnline(0);
			userinfoService.updateUserinfo(ui);
			
			//removeApplication
			 ServletContext appli = ServletActionContext.getRequest().getSession().getServletContext();
			if(appli.getAttribute(userid+"")!=null){
				appli.removeAttribute(userid+"");
			}
			//修改状态
			j.put("code", 1);
			out.print(j);
		} catch (Exception e) {
			j.put("code",-1);
			out.print(j);
		}
		Log4gUtil.Info("loginOut", userid+"", "退出登录结果",j.toString(), NF_callid, token);
	}
	
	public void setUserid(int userid) {
		this.userid = userid;
	}

	public void setUserinfoService(UserinfoService userinfoService) {
		this.userinfoService = userinfoService;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setuserinfoService(UserinfoService userinfoService) {
		this.userinfoService = userinfoService;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public void setAreaIpService(AreaIpService areaIpService) {
		this.areaIpService = areaIpService;
	}

	public int getUserid() {
		return userid;
	}

	public String getIp() {
		return ip;
	}

	public void setDealService(DealService dealService) {
		this.dealService = dealService;
	}

	public UserinfoService getUserinfoService() {
		return userinfoService;
	}

	public AreaIpService getAreaIpService() {
		return areaIpService;
	}

	public UseraccountService getUseraccountService() {
		return useraccountService;
	}
	
	public String getParams() {
		return params;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setMapService(MapService mapService) {
		this.mapService = mapService;
	}

	public void setNF_callid(String nF_callid) {
		NF_callid = nF_callid;
	}

	
}
